package ch.bfh.fbi.mobicomp.zap03;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.simplix.android.speech.string.FrenchString;
import ch.simplix.android.speech.string.GermanString;
import ch.simplix.android.speech.string.UKString;
/**
 * In Zap02 we have seen how to do somewhat more complex stuff.
 * So let us get a little bit more serious about it. What if we want to create an application that runs longer than just two seconds?
 * @author SimpliX
 *
 */
public class Zap03MessageBroadcastReceiver extends BroadcastReceiver {
	private static final String TAG = "Zap03MessageBroadcastReceiver";
	private static final String destructionKey = "destroyMe";
	private static final String haltKey = "stopDestroyMe";

	private Intent speechIntent = new Intent(
			"ch.simplix.android.speech.SPEECH_SERVICE");
	private long countdown;

	private Context context;

	@Override
	public void onReceive(Context context, Intent intent) {

		this.context = context;

		if (intent.getExtras().getBoolean(destructionKey)) {
			Log.d(TAG, "destruction initiated...");
			initiateAutoDestruction();

		} else if (intent.getExtras().getBoolean(haltKey)) {
			Log.d(TAG, "destruction aborted...");
			resetAutoDestruction();

		} else {
			Log.d(TAG, "unknown command.");
		}

	}

	private void resetAutoDestruction() {
		if (countdown == 0)
			return;
		Thread.currentThread().interrupt();
		countdown = 0;

		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence aborted."));
		context.startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungssequenz abgebrochen."));
		context.startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction avortée."));
		context.startService(speechIntent);

	}

	private void initiateAutoDestruction() {
		if (countdown != 0)
			return;
		countdown = System.currentTimeMillis() + 3 * 60 * 1000;
		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence initiated."));
		context.startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungs Sequenz eingeleitet."));
		context.startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction declanchée."));
		context.startService(speechIntent);
		countDown();
	}

	private void countDown() {
		try {
			int timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
			while (timeLeft > 70) {
				minutesCount(timeLeft);
				Thread.sleep(60000); // 1 Minute
				timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
			}
			while (timeLeft > 11) {
				secondsBlockCount(timeLeft);
				Thread.sleep(10000); // 10 Seconds
				timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
			}
			while (timeLeft > 0) {
				secondsCount(timeLeft);
				Thread.sleep(1000); // 1 Second
				timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
			}
			destroy();
		} catch (InterruptedException ex) {
		}
	}

	private void minutesCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString(""
				+ (1 + (timeLeft / (60))) + "Minutes until self destruction"));
		context.startService(speechIntent);
		speechIntent
				.putExtra("Phrase", new GermanString(""
						+ (1 + (timeLeft / (60)))
						+ "Minuten bis zur Selbstzerstörung"));
		context.startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(""
				+ (1 + (timeLeft / (60)))
				+ "minutes jusqu'à ce que l'auto-destruction"));
		context.startService(speechIntent);
	}

	private void secondsBlockCount(int timeLeft) {
		if (timeLeft % 3 == 0) {
			speechIntent.putExtra("Phrase", new UKString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Seconds until self destruction"));
		} else if (timeLeft % 3 == 1) {
			speechIntent.putExtra("Phrase", new GermanString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Sekunden bis zur Selbstzerstörung"));
		} else {
			speechIntent.putExtra("Phrase", new FrenchString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "seconds jusqu'à ce que l'auto-destruction"));
		}
		context.startService(speechIntent);
	}

	private void secondsCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString("" + (timeLeft)));
		context.startService(speechIntent);
	}

	private void destroy() {
		countdown = 0;
		speechIntent.putExtra("Phrase", new UKString("Self destroying now."));
		context.startService(speechIntent);
	}

}
